package com.wwl.cms.config.elasticsearch;

import com.wwl.cms.basic.JSON;
import com.wwl.cms.basic.JSONObject;
import com.wwl.cms.dto.AuditPoiVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
//import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.Fuzziness;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.metrics.Avg;
import org.elasticsearch.search.aggregations.metrics.Max;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.BeanUtils;

import java.io.IOException;
import java.util.Date;

/**
 * @author wwl
 * @version 1.0
 * @date 2021/10/22 17:50
 * @description:高级查询
 */
@Slf4j
public class EsAdvancedQuery {
    public static void main(String[] args) {
        try {
            RestHighLevelClient client = new RestHighLevelClient(
                    RestClient.builder(new HttpHost("localhost", 9200, "http"))
            );

           /* AuditPoiV auditPoi = new AuditPoi();
            auditPoi.setId(88888889);
            auditPoi.setName("测试1234");
            auditPoi.setStatus(1);
            auditPoi.setCreateTime(new Date());

            IndexRequest request = new IndexRequest("audit_poi");
            request.id("8888888900");
            request.source(JSON.toJSONString(auditPoi), XContentType.JSON);
            try {
                IndexResponse response = client.index(request, RequestOptions.DEFAULT);
                log.info("返回结果：{}",JSON.toJSONString(response));
            } catch (IOException e) {
                e.printStackTrace();
            }*/


            //1.请求体查询
           /* //查询所有索引数据
            SearchRequest request = new SearchRequest();
            request.indices("user");
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();*/
            /*sourceBuilder.query(QueryBuilders.matchAllQuery());//查询所用数据
            //sourceBuilder.query(QueryBuilders.termQuery("age","20"));//erm查询,条件为关键字
            //分页
            sourceBuilder.from(2);//当前页其实索引(第一条数据的顺序号)
            sourceBuilder.size(2);
            //排序
            sourceBuilder.sort("age", SortOrder.DESC);
            //过滤字段
            String[] excludes = {};
            String[] includes = {"name","age"};
            sourceBuilder.fetchSource(includes,excludes);*/


            //Bool查询
          /*  BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.must(QueryBuilders.matchQuery("age",20));//必须包含
            boolQueryBuilder.mustNot(QueryBuilders.matchQuery("name","zhangsan"));//一定不含
            boolQueryBuilder.should(QueryBuilders.matchQuery("sex","女"));//可能包含
            sourceBuilder.query(boolQueryBuilder);*/

            //范围查询
           /* RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("age");
            rangeQuery.gte("18");//大于等于
            rangeQuery.lte("19");//小于等于
            sourceBuilder.query(rangeQuery);*/

            //模糊查询
            /*sourceBuilder.query(QueryBuilders.fuzzyQuery("name","zhangsan")
                    .fuzziness(Fuzziness.TWO));//模糊几位*/

            //高亮查询
           /* TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name","zhangsan");
            sourceBuilder.query(termQueryBuilder);
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            highlightBuilder.preTags("<font color='red'>");//设置标签前缀
            highlightBuilder.postTags("</font>");
            highlightBuilder.field("name");//设置高亮字段
            sourceBuilder.highlighter(highlightBuilder);*/

            //聚合查询
            //(最大年龄)
            //sourceBuilder.aggregation(AggregationBuilders.max("maxAge").field("age"));

            //分组统计
          /*  sourceBuilder.aggregation(AggregationBuilders.terms("age_groupby").field("age"));

            request.source(sourceBuilder);
            SearchResponse response = client.search(request, RequestOptions.DEFAULT);
            log.info("返回结果:{}", JSON.toJSONString(response));
            SearchHits hits = response.getHits();
            log.info("took:{};timeout:{};total:{};MaxScore:{};",response.getTook(),response.isTimedOut(),hits.getTotalHits(),hits.getMaxScore());
            for (SearchHit hit:hits){
                log.info(hit.getSourceAsString());
            }
            client.close();*/
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
